#!/bin/sh
#
# description: Starts User mounts
#
mos=/usr/local/etc/mos/mounts

case "$1" in
  start)
	echo "Starting user mounts..."

	# waiting valid IP address
	while [ -z "$(ifconfig | sed -n '/127.0.0.1/d;/192.168.0.9/d;s/^ *inet addr:\([^ ]*\).*$/\1/p' )" ] ; do
	  sleep 5
	done

	touch /tmp/mounts.pid

	if [ -f $mos/mounts.conf ] ; then
	
	  cIFS=$IFS
	  IFS="
"
	  str=$( cat $mos/mounts.conf )
	  for i in $str ; do
	    cPoint=$( echo "$i" | cut -d"	" -f 1 )
	    cDevice=$( echo "$i" | cut -d"	" -f 2 )
	    cFS=$( echo "$i" | cut -d"	" -f 3 )
	    cOption=$( echo "$i" | cut -d"	" -f 4 )
	    cLabel=$( echo "$i" | cut -d"	" -f 5 )

	    IFS=$cIFS

	    [ ! -z "$cOption" ] && cOption=" -o $cOption"

	    [ ! -d "$cPoint" ] && mkdir -p "$cPoint"
	    [ -d "$cPoint" ] && mount -t $cFS$cOption "${cDevice}" "${cPoint}"

	  done
	fi
	;;
  stop)
	echo "Stopping user mounts..."

	if [ -f $mos/mounts.conf ] ; then

	  cIFS=$IFS
	  IFS="
"
	  str=$( cat $mos/mounts.conf )
	  for i in $str ; do
	    cPoint=$( echo "$i" | cut -d"	" -f 1 )

	    IFS=$cIFS

	    umount "$cPoint"
	    rmdir "$cPoint"

	  done
	fi

	rm -f /tmp/mounts.pid
	;;
  status)
	if [ ! -f /tmp/mounts.pid ] ; then
		echo "Mounts is stopped"
	else
		echo "Mounts is running"
	fi
	
	;;
  restart | enable | disable)
	;;
  *)
	echo "Usage: $0 {start|stop|restart|status|enable|disable}"
esac

exit $?
